<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:pm="livcos.org/ns/pm"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="doc pm f">
	
	<xsl:import href="/livcos.org/style/base.xsl"/>
	
	<xsl:param name="ref"/>
	
	<xsl:variable name="tasks" select="doc(concat($ref, '.content'))/*//pm:task"/>

	<xsl:template match="/">
		<xsl:variable name="data">
			<xsl:apply-templates select="$tasks" mode="prep"/>
		</xsl:variable>
		<table style="font-size:80%">
			<thead>
				<tr style="background-color: gray; color: white">
					<th>Task</th>
					<th>Est.</th>
					<th>Eff.</th>
					<th>Prog.</th>
				</tr>
			</thead>
			<tbody>
				<xsl:apply-templates select="$data/*">
					<xsl:sort select="@priority"/>
					<xsl:sort select="pm:title"/>
				</xsl:apply-templates>
			</tbody>
			<tbody>
				<tr style="background-color: gray; color: white">
					<td>Total</td>
					<xsl:variable name="ts" select="sum($data/pm:task/@estimate)"/>
					<xsl:variable name="tf" select="sum($data/pm:task/@effort)"/>
					<xsl:variable name="ta" select="sum($data/pm:task/@actual-estimate)"/>
					<td style="text-align: right">
						<xsl:if test="$ta != $ts">
							<xsl:attribute name="title">Actual total estimate: <xsl:value-of select="$ta"
							/>h</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="$ts"/>
					</td>
					<td style="text-align: right"><xsl:value-of select="$tf"/></td>
					<td style="text-align: right" title="Actual total progress"><xsl:value-of
							select="round($tf div $ta * 100)"/>%</td>
				</tr>
			</tbody>
		</table>
	</xsl:template>
	
	<xsl:template match="pm:task">
		<xsl:if test="not(../pm:task//*/@id = @id)">
			<tr id="{@id}" class="task task_p{@priority}">
				<td style="padding: 0px 2px">
					<xsl:if test="doc:paragraph">
						<xsl:attribute name="title" select="doc:paragraph[1]"/>
					</xsl:if>
					<xsl:for-each select="../ancestor::pm:task">&#160;&#160;</xsl:for-each>
					<xsl:if test="@dependency">
						<span id="{@dependency}">&#160;&#160;</span>
					</xsl:if>
					<xsl:value-of select="pm:title"/>
				</td>
				<td style="text-align: right">
					<xsl:if test="number(@actual-estimate) &gt; number(@estimate)">
						<xsl:attribute name="title">Actual estimate: <xsl:value-of select="@actual-estimate"
						/>h</xsl:attribute>
						<xsl:attribute name="style">text-align: right; color: #1122FF</xsl:attribute>
					</xsl:if>
					<xsl:if test="number(@actual-estimate) &lt; number(@estimate)">
						<xsl:attribute name="style">text-align: right; color: #008811</xsl:attribute>
						<xsl:attribute name="title">Actual effort: <xsl:value-of select="@effort"
							/>h</xsl:attribute>
					</xsl:if>
					<xsl:value-of select="@estimate"/>
				</td>
				<td style="text-align: right"><xsl:value-of select="@effort"/></td>
				<td style="text-align: right">
					<xsl:if test="@progress > 0">
						<xsl:attribute name="title"><xsl:value-of select="@progress"/>%</xsl:attribute>
						<div class="progress_box" style="width:32px">
								<div class="progress_bar" style="width:{@progress}%">
									<xsl:text>&#160;</xsl:text>
								</div>
						</div>
					</xsl:if>
				</td>
			</tr>
			<xsl:apply-templates select="pm:task">
				<xsl:sort select="@priority"/>
				<xsl:sort select="pm:title"/>
			</xsl:apply-templates>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="pm:task" mode="prep">
		<xsl:param name="dependency"/>
		<xsl:variable name="progress">
			<xsl:choose>
				<!-- progress = effort / estimate -->
				<xsl:when test="not(@progress) and (@effort > 0)">
					<xsl:value-of select="@effort div @estimate"/>
				</xsl:when>
				<!-- default progress = 0 -->
				<xsl:when test="not(@progress)">0</xsl:when>
				<!-- effort, but no progress => at least 0.1 percent -->
				<xsl:when test="(@progress = 0) and (@effort > 0)">0.001</xsl:when>
				<!-- specified progress -->
				<xsl:otherwise>
					<xsl:value-of select="@progress div 100"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="actual-estimate">
			<xsl:choose>
				<xsl:when test="($progress > 0) and (@effort > 0)">
					<xsl:value-of select="@effort div $progress"/>
				</xsl:when>
				<xsl:when test="$progress > 0">
					<xsl:value-of select="@estimate * $progress"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="@estimate"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:if test="not(@priority)"><xsl:attribute name="priority">3</xsl:attribute></xsl:if>
			<xsl:if test="@progress and not(@effort)">
				<xsl:attribute name="effort" select="@estimate * $progress"/>
			</xsl:if>
			<xsl:if test="not(@progress) and @effort">
				<xsl:attribute name="progress" select="round($progress * 100)"/>
			</xsl:if>
			<xsl:attribute name="actual-estimate" select="$actual-estimate"/>
			<xsl:if test="$dependency">
				<xsl:attribute name="dependency" select="$dependency"/>
			</xsl:if>
			<xsl:copy-of select="node() except pm:depends"/>
			<xsl:for-each select="pm:depends">
				<xsl:variable name="t" select="f:select-entity(@on, $tasks)"/>
				<xsl:apply-templates select="$t" mode="prep">
					<xsl:with-param name="dependency" select="@id"/>
				</xsl:apply-templates>
				<xsl:if test="not($t)">
					<pm:task dependency="{@id}" priority="5">
						<pm:title>depends on '<xsl:value-of select="@id"/>'...</pm:title>
					</pm:task>
				</xsl:if>
			</xsl:for-each>
		</xsl:copy>
	</xsl:template>
	
</xsl:stylesheet>
